package com.yanqu.road.server.cmd.assistant;

import com.yanqu.road.entity.assistant.AssistantSettingUserData;
import com.yanqu.road.entity.enums.eCardType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.pb.assistant.AssistantProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.assistant.AssistantModule;
import com.yanqu.road.server.gameplayer.module.player.CardModule;
import com.yanqu.road.server.manger.assistant.AssistantMgr;
import com.yanqu.road.server.manger.assistant.CrossActionHandler;
import com.yanqu.road.server.manger.assistant.handler.*;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.BitUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2021/7/29 11:03
 */
@Cmd(code = GameProtocol.S_ASSISTANT_ACTION, desc = "执行小助理操作")
public class AssistantActionCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        AssistantProto.AssistantActionReqMsg.Builder reqMsg = AssistantProto.AssistantActionReqMsg.parseFrom(packet.getBytes()).toBuilder();

        if (!reqMsg.getIsOnlySave()) {//要执行的才判断系统解锁
            if (!SystemOpenMgr.systemOpen(player, eSystemId.Assistant.getValue()) || !player.getModule(CardModule.class).isInCardTime(eCardType.MonthCard.getValue())){
                ret = GameErrorCode.E_TITLE_LEVEL_NO_ENOUGH;
                return;
            }
        }
        AssistantProto.AssistantActionRespMsg.Builder respMsg = AssistantProto.AssistantActionRespMsg.newBuilder();
        respMsg.setRet(0);
        if (!reqMsg.getIsOnlySave()) {
            // 小助手操作
            new AssistantCrossHandler().handle(player, reqMsg, respMsg);
        } else {
            // 直接回包，下面设置走同步
            player.sendPacket(ClientProtocol.U_ASSISTANT_ACTION, respMsg);
        }

        AssistantSettingUserData settingUserData = player.getModule(AssistantModule.class).getSettingUserData();
        if (settingUserData != null) {
            // 执行操作
            settingUserData.setOperateTypeList(reqMsg.getOperateEnumList());
            if (reqMsg.hasCharitableTimes()) {
                settingUserData.setCharitableTimes(reqMsg.getCharitableTimes());
            }
            if (reqMsg.hasId()) {
                settingUserData.setBeautyId(reqMsg.getId());
            }
            Map<Integer, Integer> enterSchoolTempMap = new ConcurrentHashMap<>();
            for (AssistantProto.AssistantGrandChildEnterSchoolItem enterSchoolItem : reqMsg.getGrandChildEnterSchoolList()) {
                enterSchoolTempMap.put(enterSchoolItem.getSchoolType(), enterSchoolItem.getPatronsId());
            }
            settingUserData.setGrandChildEnterSchoolMap(enterSchoolTempMap);
            if (reqMsg.hasOpenComprehend()) {
                settingUserData.setOpenComprehend(reqMsg.getOpenComprehend());
            }
            if (reqMsg.hasBeautyFavorHundred()) {
                settingUserData.setBeautyFavorHundred(reqMsg.getBeautyFavorHundred());
            }

            // 更新完同步下
            player.getModule(AssistantModule.class).syncAssistantSetting();
        }

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0) {
            AssistantProto.AssistantActionRespMsg.Builder respMsg = AssistantProto.AssistantActionRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_ASSISTANT_ACTION, respMsg);
        }
    }
}
